package com.techsoft.pool;

import com.techsoft.ConnectionPool;
import com.techsoft.ConnectionPoolException;

public class ConnectionPoolFactory {
	private static final String default_ConnectionPool_class = "com.techsoft.pool.C3P0ConnectionPool";
	private static String current_cache_class = null;
	private static ConnectionPoolFactory instance = null;
	private ConnectionPool pool = null;

	public static String getCurrent_cache_class() {
		return current_cache_class;
	}

	public static void setCurrent_cache_class(String current_cache_class) {
		ConnectionPoolFactory.current_cache_class = current_cache_class;
	}

	private ConnectionPoolFactory() {
	}

	public static ConnectionPoolFactory getInstance() {
		if (instance == null) {
			instance = new ConnectionPoolFactory();
		}

		return instance;
	}

	public ConnectionPool getConnectionPool() throws ConnectionPoolException {
		String currentclass = null;
		if (pool == null) {
			currentclass = ConnectionPoolFactory.current_cache_class;
			if (currentclass == null) {
				currentclass = ConnectionPoolFactory.default_ConnectionPool_class;
			}
			try {
				Class<?> cls = Class.forName(currentclass);
				pool = (ConnectionPool) cls.newInstance();
			} catch (ClassNotFoundException e) {
				throw new ConnectionPoolException(e.getMessage(), e);
			} catch (InstantiationException e) {
				throw new ConnectionPoolException(e.getMessage(), e);
			} catch (IllegalAccessException e) {
				throw new ConnectionPoolException(e.getMessage(), e);
			}
		}

		return pool;
	}
}
